package com.weijianhuawen.wjblog.admin.service.impl;

import com.weijianhuawen.wjblog.admin.mode.vo.file.UploadFileRspVo;
import com.weijianhuawen.wjblog.admin.service.AdminFileService;
import com.weijianhuawen.wjblog.common.utils.MinioUtil;
import com.weijianhuawen.wjblog.common.enums.ResponseCodeEnum;
import com.weijianhuawen.wjblog.common.exception.BizException;
import com.weijianhuawen.wjblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-23 19:44
 */

@Service
@Slf4j
public class AdminFileServiceImpl implements AdminFileService {
    //Minio工具
    @Resource
    private MinioUtil minioUtil;
    @Override
    public Response uploadFile(MultipartFile file) {
        try {
            //上传文件
            String url = minioUtil.uploadFile(file);

            //构建响应参数
            return Response.success(UploadFileRspVo.builder().url(url).build());
        } catch (BizException e) {
            log.warn("【文件上传服务】 文件上传异常");
            throw e;
        } catch (Exception e) {
            log.error("【文件上传服务】 文件上传未知异常");
            throw new BizException(ResponseCodeEnum.UPLOAD_FILE_UNKNOW_ERROR);
        }
    }
}
